package list.skiplist2;

import java.util.Random;
import java.util.stream.IntStream;

/**
 * @author: 26233
 * @create: 2022-08-03 15:16:49
 * @Description
 *
 * 跳表结构
 *
 **/

public class SKList {
    private SKHeadNode topHead;
    private SKHeadNode tailHead;
    private int level;
    static Random random = new Random();

    public SKList() {
        topHead = tailHead = new SKHeadNode();
    }

    public void insert(int v) {
        SKNode newSKNode = new SKNode(v);
        // 最底层插入,从小到大
        if (tailHead.getRight() == null) {
            tailHead.setRight(newSKNode);
        }else {
            SKNode tempSKNode = tailHead.getRight();
            while (tempSKNode.getRight() != null && tempSKNode.getV() <= newSKNode.getV()) {
                tempSKNode = tempSKNode.getRight();
            }
            if (tempSKNode.getRight() != null) {
                newSKNode.setRight(tempSKNode.getRight());
            }
            tempSKNode.setRight(newSKNode);
        }
        // 提升level
        SKHeadNode tempSKHeadNode = tailHead;
        int i = random.nextInt();
        while (i % 2 == 0) {
            System.out.println("i = " + i);
            i = random.nextInt();
            SKNode newLevelNode = new SKNode(v);
            // 新增一层，直接结束
            if (tempSKHeadNode.getUp() == null) {
                SKHeadNode skHeadNode = new SKHeadNode();
                skHeadNode.setDown(tempSKHeadNode);
                skHeadNode.getDown().setUp(skHeadNode);
                topHead = skHeadNode;
                topHead.setRight(newLevelNode);
                newLevelNode.setDown(newSKNode);
                break;
            }else {
                // 往上一层
                tempSKHeadNode = tempSKHeadNode.getUp();

                SKNode right = tempSKHeadNode.getRight();
                SKNode preRight = null;
                while (right != null && right.getV() <= newSKNode.getV()) {
                    preRight = right;
                    right = right.getRight();
                }
                if (right == null) {// 尾部提升
                    if (preRight == null) {
                        tempSKHeadNode.setRight(newLevelNode);
                        newLevelNode.setDown(newSKNode);
                        break;
                    }else {
                        preRight.setRight(newLevelNode);
                        newLevelNode.setDown(newSKNode);
                    }
                }else {// 中间位置提升
                    if (preRight == null) {
                        newLevelNode.setRight(tempSKHeadNode.getRight());
                        tempSKHeadNode.setRight(newLevelNode);
                    }else {
                        newLevelNode.setRight(preRight.getRight());
                        preRight.setRight(newLevelNode);
                    }
                    newLevelNode.setDown(newSKNode);
                }
            }
            newSKNode = newLevelNode;
        }
        // 不提升
    }

    public static void main(String[] args) {

        SKList skList = new SKList();
        IntStream.range(1, 15).forEach(skList::insert);
        System.out.println("skList = " + skList);

    }

}
